﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class PointerClickListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
    public delegate void VoidDelegate(GameObject go, BaseEventData eventData);
    public VoidDelegate onClick;
    public VoidDelegate onPointerDown;
    public VoidDelegate onPointerUp;
    public static PointerClickListener Get(GameObject go)
	{
		PointerClickListener listener = go.GetComponent<PointerClickListener>();
		if (listener == null) listener = go.AddComponent<PointerClickListener>();
		return listener;
	}

    public static PointerClickListener Get(MonoBehaviour component)
	{
		return Get(component.gameObject);
	}

    public void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick(gameObject, eventData);
	}

    public void OnPointerDown(PointerEventData eventData)
    {
        if (onPointerDown != null) onPointerDown(gameObject, eventData);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (onPointerUp != null) onPointerUp(gameObject, eventData);
    }
    // --------------------------------------------------------------------------------------------
    // 扩展接口
    public static void AddClick(GameObject go, VoidDelegate onClick)
    {
        Get(go).onClick += onClick;
    }

	public static void AddClick(Component component, VoidDelegate onClick)
    {
        AddClick(component.gameObject, onClick);
    }

    public static void AddPointerDown(GameObject go, VoidDelegate onPointerDown)
    {
        Get(go).onPointerDown += onPointerDown;
    }

	public static void AddPointerDown(Component component, VoidDelegate onPointerDown)
    {
        AddPointerDown(component.gameObject, onPointerDown);
    }

    public static void AddPointerUp(GameObject go, VoidDelegate onPointerUp)
    {
        Get(go).onPointerUp += onPointerUp;
    }

	public static void AddPointerUp(Component component, VoidDelegate onPointerUp)
    {
        AddPointerUp(component.gameObject, onPointerUp);
    }

    public static void RemoveClick(GameObject go, VoidDelegate onClick)
    {
        Get(go).onClick -= onClick;
    }

	public static void RemoveClick(Component component, VoidDelegate onClick)
    {
        RemoveClick(component.gameObject, onClick);
    }

    public static void RemovePointerDown(GameObject go, VoidDelegate onPointerDown)
    {
        Get(go).onPointerDown -= onPointerDown;
    }

	public static void RemovePointerDown(Component component, VoidDelegate onPointerDown)
    {
        RemovePointerDown(component.gameObject, onPointerDown);
    }

    public static void RemovePointerUp(GameObject go, VoidDelegate onPointerUp)
    {
        Get(go).onPointerUp -= onPointerUp;
    }

	public static void RemovePointerUp(Component component, VoidDelegate onPointerUp)
    {
        RemovePointerUp(component.gameObject, onPointerUp);
    }
}